const Article = require('../models/articleModel')

class ArticleController {
    /**
     * @swagger
     * /article/getClassifyList:
     *   get:
     *     summary: 获取文章分类
     *     description: 获取文章分类
     *     tags: [获取文章分类]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: [{id: 1,name: "分类名称"}]
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取文章分类
    static async getClassifyList(ctx) {
        try {
            const result = await Article.getClassifyList()

            if (result) {
                if (result.length === 0) {
                    ctx.body = {
                        code: 200,
                        data: [],
                        msg: '查询成功，但无数据！'
                    }
                    return
                }
                ctx.body = {
                    code: 200,
                    data: result,
                    msg: '查询成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '查询失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/addArticle:
     *   post:
     *     summary: 添加文章
     *     description: 添加文章
     *     tags: [添加文章]
     *     parameters:
     *       - name: classifyId
     *         type: string | number
     *         required: true
     *         description: 分类id
     *       - name: title
     *         type: string
     *         required: true
     *         description: 文章标题
     *       - name: abstract
     *         type: string
     *         required: true
     *         description: 文章摘要
     *       - name: content
     *         type: string
     *         required: true
     *         description: 文章内容
     *       - name: state
     *         type: number
     *         required: true
     *         description: 状态：0 草稿，1 待审核，2 审核通过，3 已发布，4 审核拒绝
     *       - name: coverUrl
     *         type: string
     *         required: false
     *         description: 文章封面图片地址（最多一张图）
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: null
     *                 msg:
     *                   type: string
     *                   example: "添加成功！"
     */
    // 添加文章
    static async addArticle(ctx) {
        try {
            const { title, content, abstract, classifyId, state, coverUrl } = ctx.request.body
            const userId = ctx.state.user.userId

            if (!userId) {
                ctx.body = {
                    code: 400,
                    data: null,
                    msg: '参数错误，请登录！'
                }
                return
            }

            if (!title || !content || !classifyId || !abstract) {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '文章标题、文章分类、文章摘要、文章内容均不能为空！'
                }
                return
            }

            // 0 草稿，1 待审核，2 审核通过，3 已发布，4 审核拒绝
            if (JSON.parse(state) !== 0 && JSON.parse(state) !== 1) {
                ctx.body = {
                    code: 400,
                    data: null,
                    msg: 'state 参数错误！'
                }
                return
            }

            let result = await Article.addArticle({ title, abstract, content, classifyId, userId, state, coverUrl })

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }
                
                ctx.body = {
                    code: 200,
                    data: null,
                    msg: '添加成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '添加失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
            return
        }
    }

    /**
     * @swagger
     * /article/delArticle:
     *   delete:
     *     summary: 删除文章
     *     description: 删除文章
     *     tags: [删除文章]
     *     parameters:
     *       - name: articleId
     *         type: string | number
     *         required: true
     *         description: 文章id
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: null
     *                 msg:
     *                   type: string
     *                   example: "文章删除成功！"
     */
    // 删除文章
    static async delArticle(cxt) {
        const articleId = cxt.request.body.articleId
        const userId = cxt.state.user.userId

        try {
            let result = await Article.delArticle(userId, articleId)

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }
                
                cxt.body = {
                    code: 200,
                    data: null,
                    msg: '文章删除成功！'
                }
                return
            }

            cxt.body = {
                code: 500,
                data: null,
                msg: '文章删除失败！'
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
            return
        }
    }

    /**
     * @swagger
     * /article/updateArticle:
     *   put:
     *     summary: 修改文章
     *     description: 修改文章
     *     tags: [修改文章]
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *       - name: title
     *         type: string
     *         required: true
     *         description: 文章标题
     *       - name: content
     *         type: string
     *         required: true
     *         description: 文章内容
     *       - name: abstract
     *         type: string
     *         required: true
     *         description: 文章摘要
     *       - name: classifyId
     *         type: number
     *         required: true
     *         description: 分类id
     *       - name: state
     *         type: number
     *         required: true
     *         description: 状态，根据文章自身状态传值
     *       - name: coverUrl
     *         type: string
     *         required: false
     *         description: 文章封面图片地址
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: NULL
     *                   example: null
     *                 msg:
     *                   type: string
     *                   example: "文章修改成功！"
     */
    // 修改文章
    static async updateArticle(ctx) {
        const { articleId, title, content, abstract, classifyId, state, coverUrl } = ctx.request.body
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '请先登录！'
            }
            return
        }

        if (!userId || !articleId || !title || !content || !abstract || !classifyId || (state !== undefined && state === null && state === '')) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '缺少参数，请检查！'
            }
            return
        }

        try {
            let result = await Article.updateArticle(userId, articleId, title, abstract, content, classifyId, state, coverUrl)

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }

                ctx.body = {
                    code: 200,
                    data: null,
                    msg: '文章修改成功！'
                }
                return
            }
            
            cxt.body = {
                code: 500,
                data: null,
                msg: '文章修改失败！'
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
            return
        }
    }

    /**
     * @swagger
     * /article/getArticleList:
     *   get:
     *     summary: 获取文章列表
     *     description: 获取文章列表
     *     tags: [获取文章列表]
     *     parameters:
     *       - name: classifyId
     *         type: number
     *         required: false
     *         description: 分类Id
     *       - name: keyword
     *         type: string
     *         required: false
     *         description: 搜索关键词
     *       - name: pageSize
     *         type: number
     *         required: false
     *         description: 分页：每一页显示多少条数据
     *       - name: currentPage
     *         type: number
     *         required: false
     *         description: 分页：当前第几页
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: { total: 0, rows: [] }
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取文章列表
    static async getArticleList(ctx) {
        const { currentPage, pageSize, classifyId, keyword } = ctx.request.query

        try {
            const result = await Article.getArticleList(currentPage, pageSize, classifyId, keyword)

            if (result) {
                ctx.body = {
                    code: 200,
                    data: {
                        rows: result.rows,
                        total: result.total
                    },
                    msg: '文章查询成功！'
                }
                return
            }

            ctx.body = {
                code: 500,
                data: null,
                msg: '文章查询失败！'
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/getArticleDetail:
     *   get:
     *     summary: 获取文章详情
     *     description: 获取文章详情
     *     tags: [获取文章详情]
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *       - name: userId
     *         type: number
     *         required: true
     *         description: 该文章的用户（作者）id
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: {
     *                      "username": "作者账号", 
     *                      "nickName": "作者昵称",
     *                      "avatar": "作者头像",
     *                       "job": "作者职位",
     *                       "title": "文章标题",
     *                       "content": "文章内容",
     *                       "createTime": "文章创建时间",
     *                       "userId": "文章作者（id）",
     *                       "state": "文章状态：3 已发布",
     *                       "likeCount": "文章点赞量",
     *                       "viewerCount": "文章观看数量（浏览量）",
     *                       "classifyId": "文章分类",
     *                       "abstract": "文章摘要",
     *                       "articleCount": "这个用户的总文章数量",
     *                       "followCount": "关注数量（粉丝数量）",
     *                       "viewerTotalCount": "文章总数量（用户的文章总数）",
     *                       "coverUrl": "文章封面图片"
     *                    }
     *                 msg:
     *                   type: string
     *                   example: "获取文章详情成功！"
     */
    // 获取文章详情
    static async getArticleDetail(ctx) {
        try {
            const { articleId, userId } = ctx.request.query

            if (!articleId && !userId) {
                ctx.status = 400
                ctx.body = {
                    code: 400,
                    data: null,
                    msg: '参数错误！'
                }

                return
            }
            
            const data = await Article.getArticleDetail(articleId, userId)

            if (data) {
                ctx.body = {
                    code: 200,
                    data,
                    msg: '获取文章详情成功！'
                }

                return
            }

            ctx.body = {
                code: 500,
                data: null,
                msg: '获取文章详情失败！'
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/addViewCount:
     *   post:
     *     summary: 添加文章观看次数
     *     description: 添加文章观看次数
     *     tags: [添加文章观看次数]
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example:
     *                 msg:
     *                   type: string
     *                   example: "操作成功！"
     */
    // 添加文章观看次数
    static async addViewCount(ctx) {
        const articleId = ctx.request.body.articleId
        const userId = ctx.state.user.userId

        if (!articleId && !userId) {
            ctx.body = {
                code: 400,
                data: null,
                msg: '参数错误！'
            }
            return
        }

        try {
            const result = await Article.addViewCount(articleId, userId)

            if (result) {
                if (result.code === 500) {
                    ctx.body = {
                        code: 200,
                        data: null,
                        msg: '该用户已经观看过该文章！'
                    }
                } else {
                    ctx.body = {
                        code: 200,
                        data: null,
                        msg: '操作成功！'
                    }
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '操作失败，请联系管理员！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/getUserArticleList:
     *   get:
     *     summary: 获取用户文章列表
     *     description: 获取用户文章列表
     *     tags: [获取用户文章列表]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: state
     *         type: number
     *         required: false
     *         description: 当前用户文章状态：0 草稿，1 待审核，2 审核通过，3 已发布，4 审核拒绝
     *       - name: pageSize
     *         type: number
     *         required: true
     *         description: 分页：每一页显示多少条数据
     *       - name: currentPage
     *         type: number
     *         required: true
     *         description: 分页：当前第几页
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: object
     *                   example: { rows: [], total: 0 }
     *                 msg:
     *                   type: string
     *                   example: "文章查询成功！"
     */
    // 获取用户文章列表
    static async getUserArticleList(ctx) {
        try {
            const { state, currentPage, pageSize } = ctx.request.query
            const userId = ctx.state.user.userId
            const result = await Article.getUserArticleList(userId, state, currentPage, pageSize)

            
            if (result) {
                ctx.body = {
                    code: 200,
                    data: result,
                    msg: '文章查询成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '文章查询失败！'
                }
            }
        } catch(error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/getArticleStateCount:
     *   get:
     *     summary: 查询用户下各个状态文章数量
     *     description: 查询用户下各个状态文章数量
     *     tags: [查询用户下各个状态文章数量]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: [{ state: 0, name: '草稿', total: 0 }, { state: 1, name: '待审核', total: 0 }, { state: 2, name: '审核通过', total: 0 }, { state: 3, name: '已发布', total: 0 }, { state: 4, name: '审核拒绝', total: 0 }]
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 查询用户下各个状态文章数量
    static async getArticleStateCount(ctx) {
        const userId = ctx.state.user.userId

        try {
            const result = await Article.getArticleStateCount(userId)

            if (result) {
                ctx.body = {
                    code: 200,
                    data: result,
                    msg: '查询成功！'
                }
                return
            }

            ctx.body = {
                code: 500,
                data: null,
                msg: '查询失败！'
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: `服务器错误：${error}，请联系管理员！`
            }
        }
    }

    /**
     * @swagger
     * /article/revokeArticle:
     *   put:
     *     summary: 撤销发布文章
     *     description: 撤销发布文章
     *     tags: [撤销发布文章]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: state
     *         type: number
     *         required: true
     *         description: 文章状态：state = 3
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "操作成功！"
     */
    // 撤销文章，将已发布的文章改为草稿
    static async revokeArticle(ctx) {
        const { articleId, state } = ctx.request.body
        const { userId } = ctx.state.user

        if ((!articleId || !state) && JSON.parse(state) !== 3) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.updateArticleState(userId, articleId, 0)

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }

                ctx.body = {
                    code: 200,
                    data: null,
                    msg: '操作成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '操作失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/revokeExamine:
     *   put:
     *     summary: 撤回审核
     *     description: 撤回审核
     *     tags: [撤回审核]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: state
     *         type: number
     *         required: true
     *         description: 文章状态：state = 1
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "操作成功！"
     */
    // 撤回审核：将待审核的文章改为草稿
    static async revokeExamine(ctx) {
        const { articleId, state } = ctx.request.body
        const { userId } = ctx.state.user

        if ((!articleId || !state) && JSON.parse(state) !== 1) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.updateArticleState(userId, articleId, 0)

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }

                ctx.body = {
                    code: 200,
                    data: null,
                    msg: '操作成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '操作失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/applyPublish:
     *   put:
     *     summary: 申请发布文章
     *     description: 申请发布文章
     *     tags: [申请发布文章]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: state
     *         type: number
     *         required: true
     *         description: 文章状态：state = 0
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "操作成功！"
     */
    // 申请发布文章
    static async applyPublish(ctx) {
        const { articleId, state } = ctx.request.body
        const { userId } = ctx.state.user

        if ((!articleId || !state) && JSON.parse(state) !== 0) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.updateArticleState(userId, articleId, 1)

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }

                ctx.body = {
                    code: 200,
                    data: null,
                    msg: '操作成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '操作失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/publishArticle:
     *   put:
     *     summary: 发布文章
     *     description: 发布文章后，用户可以从首页看到你发布的文章
     *     tags: [发布文章]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: state
     *         type: number
     *         required: true
     *         description: 文章状态：state = 2
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "文章发布成功！"
     */
    // 发布文章：将审核通过的文章发布
    static async publishArticle(ctx) {
        const { articleId, state } = ctx.request.body
        const { userId } = ctx.state.user

        if ((!articleId || !state) && JSON.parse(state) !== 2) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.updateArticleState(userId, articleId, 3)

            if (result) {
                if (result.code === 500) {
                    ctx.body = result
                    return
                }

                ctx.body = {
                    code: 200,
                    data: null,
                    msg: '文章发布成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '文章发布失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/commentArticle:
     *   post:
     *     summary: 评论文章
     *     description: 评论文章
     *     tags: [评论文章]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *       - name: uid
     *         type: number
     *         required: true
     *         description: 这篇文章作者的id
     *       - name: content
     *         type: string
     *         required: true
     *         description: 评论内容
     *       - name: picUrls
     *         type: string
     *         required: false
     *         description: 评论图片地址，多个图片用 “,” 拼接
     *       - name: parentId
     *         type: string
     *         required: false
     *         description: 父评论id，如果没有父评论，那就不用传
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "评论成功！"
     */
    // 评论文章
    static async commentArticle(ctx) {
        const { uid, articleId, content, picUrls, parentId, rootId } = ctx.request.body
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '请先登录！'
            }
            return
        }

        if (!articleId || !content) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.commentArticle({ uid, userId, articleId, content, picUrls, parentId, rootId })

            if (result) {
                ctx.body = {
                    code: 200,
                    data: result,
                    msg: '评论成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '评论失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/getCommentList:
     *   get:
     *     summary: 获取文章评论列表
     *     description: 当前文章评论的列表
     *     tags: [获取文章评论列表]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *       - name: currentPage
     *         type: number
     *         required: true
     *         description: 分页：当前页
     *       - name: pageSize
     *         type: number
     *         required: true
     *         description: 分页：每页显示的数量
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Array
     *                   example: [
     *                     {
     *                       "articleId": "文章id",
     *                       "avatar": "作者头像",
     *                       "commentId": "评论id",
     *                       "createTime": "评论时间",
     *                       "isLikeState": "评论是否点赞，0 未点赞，1 已点赞",
     *                       "nickName": "作者昵称",
     *                       "parentId": "父评论id",
     *                       "pciUrls": "评论图片内容",
     *                       "userId": "作者id",
     *                       "username": "作者账号",
     *                       "replyCount": "子评论回复总数",
     *                     }
     *                   ]
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取文章评论列表
    static async getCommentList(ctx) {
        const { articleId, currentPage, pageSize } = ctx.request.query
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '请先登录！'
            }
            return
        }

        if (!articleId) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.getCommentList(userId, articleId, currentPage, pageSize)

            if (result) {
                ctx.body = {
                    code: 200,
                    data: result,
                    msg: '查询成功！'
                }
            } else {
                ctx.body = {
                    code: 500,
                    data: null,
                    msg: '查询失败！'
                }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/getCommentTotalCount:
     *   get:
     *     summary: 获取所有父子评论总数
     *     description: 获取所有父子评论总数
     *     tags: [获取所有父子评论总数]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: Number
     *                   example: 10
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取所有父子评论总数
    static async getCommentTotalCount(ctx) {
        const { articleId } = ctx.request.query
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = { code: 500, data: null, msg: '请登录！' }
            return
        }

        try {
            const result = await Article.getCommentTotalCount(articleId)

            if (result) {
                ctx.body = { code: 200, data: result[0].total, msg: '查询成功！' }
                return
            }

            ctx.body = { code: 500, data: null, msg: '查询失败！' }
        } catch (err) {
            ctx.body = { code: 500, data: null, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /article/delComment:
     *   delete:
     *     summary: 删除文章评论
     *     description: 删除文章评论
     *     tags: [删除文章评论]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *       - name: commentId
     *         type: number
     *         required: true
     *         description: 评论的id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "删除成功！"
     */
    // 删除文章评论（只能删除登录人自己的评论）
    static async delComment(ctx) {
        const { commentId, articleId } = ctx.request.body
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '请先登录！'
            }
            return
        }

        if (!commentId || !articleId) {
            ctx.body = {
                code: 500,
                data: null,
                msg: '参数错误，请检查！'
            }
            return
        }

        try {
            const result = await Article.delComment(commentId, articleId, userId)

            if (result.code === 500) {
                ctx.body = result
                return
            }

            ctx.body = {
                code: 200,
                data: null,
                msg: '删除成功！'
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = {
                code: 500,
                data: null,
                msg: '服务器错误，请联系管理员！'
            }
        }
    }

    /**
     * @swagger
     * /article/likeArticle:
     *   post:
     *     summary: 给文章点赞&取消点赞
     *     description: 此接口既可以点赞，也可以取消点赞，参数一致
     *     tags: [给文章点赞&取消点赞]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "点赞成功！"
     */
    // 给文章点赞&取消点赞
    static async likeArticle(ctx) {
        const { articleId } = ctx.request.body
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = { code: 500, data: null, msg: '请先登录！' }
            return
        }

        if (!articleId) {
            ctx.body = { code: 500, data: null, msg: '参数错误，请检查！' }
            return
        }

        try {
            const result = await Article.likeArticle(articleId, userId)
            ctx.body = result
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, data: null, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /article/getLikeArticleState:
     *   get:
     *     summary: 获取用户是否给文章点赞
     *     description: 此接口在使用时，需要先登录，如果用户没有登录，就不用调这个接口
     *     tags: [获取用户是否给文章点赞]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: true 用户已给文章点赞 or false 用户没有给文章点赞
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取用户是否给文章点赞
    static async getLikeArticleState(ctx) {
        const { articleId } = ctx.request.query
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = { code: 500, data: null, msg: '请先登录！' }
            return
        }

        if (!articleId) {
            ctx.body = { code: 500, data: null, msg: '参数错误，请检查！' }
            return
        }

        try {
            const result = await Article.getLikeArticleState(articleId, userId)
            ctx.body = result
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, data: null, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /article/likeComment:
     *   post:
     *     summary: 给评论点赞&取消点赞
     *     description: 此接口既可以点赞，也可以取消点赞，参数一致
     *     tags: [给评论点赞&取消点赞]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: articleId
     *         type: number
     *         required: true
     *         description: 文章id
     *       - name: commentId
     *         type: number
     *         required: true
     *         description: 评论id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: 
     *                 msg:
     *                   type: string
     *                   example: "点赞成功！"
     */
    // 给评论点赞&取消点赞
    static async likeComment(ctx) {
        const { articleId, commentId } = ctx.request.body
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = { code: 500, data: null, msg: '请先登录！' }
            return
        }

        if (!articleId || !commentId) {
            ctx.body = { code: 500, data: null, msg: '参数错误，请检查！' }
            return
        }

        try {
            const result = await Article.likeComment(articleId, commentId, userId)
            ctx.body = result
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, data: null, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /article/getLikeCommentState:
     *   get:
     *     summary: 获取用户是否给评论点赞
     *     description: 此接口在使用时，需要先登录，如果用户没有登录，就不用调这个接口
     *     tags: [获取用户是否给评论点赞]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: commentId
     *         type: number
     *         required: true
     *         description: 评论id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: true 用户已给评论点赞 or false 用户没有给评论点赞
     *                 msg:
     *                   type: string
     *                   example: "点赞成功！"
     */
    // 获取用户是否给评论点赞
    static async getLikeCommentState(ctx) {
        const { commentId } = ctx.request.query
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = { code: 500, data: null, msg: '请先登录！' }
            return
        }

        if (!commentId) {
            ctx.body = { code: 500, data: null, msg: '参数错误，请检查！' }
            return
        }

        try {
            const result = await Article.getLikeCommentState(commentId, userId)
            ctx.body = result
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, data: null, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /article/reportComment:
     *   post:
     *     summary: 举报文章评论
     *     description: 举报文章评论
     *     tags: [文章相关]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: commentId
     *         type: number
     *         required: true
     *         description: 评论id
     *       - name: content
     *         type: string
     *         required: true
     *         description: 举报内容
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: null
     *                   example: true 用户已给评论点赞 or false 用户没有给评论点赞
     *                 msg:
     *                   type: string
     *                   example: "点赞成功！"
     */
    // 举报文章评论
    static async reportComment(ctx) {
        const { articleId, commentId, content } = ctx.request.body
        const { userId } = ctx.state.user

        if (!userId) {
            ctx.body = { code: 500, data: null, msg: '请先登录！' }
            return
        }

        if (!articleId || !commentId || !content) {
            ctx.body = { code: 500, data: null, msg: '参数错误，请检查！' }
            return
        }

        try {
            const result = await Article.reportComment(articleId, commentId, content, userId)
            ctx.body = result
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, data: null, msg: '服务器错误，请联系管理员！' }
        }
    }
}

module.exports = ArticleController
